package com.example.service.impl;

import cn.hutool.log.Log;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.bean.Course;
import com.example.bean.StudentCourse;
import com.example.exception.ServiceException;
import com.example.service.CourseService;
import com.example.mapper.CourseMapper;
import com.example.service.StudentCourseService;
import com.example.until.ResultCodeEnum;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
* @author wjf
* @description 针对表【course】的数据库操作Service实现
* @createDate 2022-09-29 14:32:44
*/
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService{
    private static final Log LOG = Log.get();
    @Resource
    private CourseMapper courseMapper;

    @Resource
    private StudentCourseService studentCourseService;

    @Override
    public IPage<Course> findPage(IPage<Course> page, String name) {
        IPage<Course> page1 =  courseMapper.findPage(page,name);
        return page1;
    }

    @Transactional
    @Override
    public Boolean setCourseAndStudent(Integer courseId, Integer studentId) {
      QueryWrapper<StudentCourse> queryWrapper = new QueryWrapper<>();
      queryWrapper.eq("student_id",studentId)
              .eq("course_id",courseId);
        StudentCourse studentCourse = new StudentCourse();
        studentCourse.setStudentId(studentId);
        studentCourse.setCourseId(courseId);
      StudentCourse one;
        try {
            one = studentCourseService.getOne(queryWrapper);
            if (one==null){
                boolean save = studentCourseService.save(studentCourse);
                if (save){
                    return true;
                }else {
                    throw new ServiceException(ResultCodeEnum.PARAM_ERROR); //参数错误
                }
            }else {
                return false;
            }
        }catch (Exception e){
            throw new ServiceException(ResultCodeEnum.SERVICE_ERROR); //服务器异常
        }

    }
    @Transactional
    @Override
    public Boolean selectMore(List<Integer> ids, Integer studentId) {
        boolean remove;
        boolean save ;
        try {
            for (Integer id : ids ){
                QueryWrapper<StudentCourse> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("course_id",id)
                        .eq("student_id",studentId);
                remove = studentCourseService.remove(queryWrapper);
                StudentCourse studentCourse = new StudentCourse();
                studentCourse.setStudentId(studentId);
                studentCourse.setCourseId(id);
                save = studentCourseService.save(studentCourse);
                if (!save){
                    throw new ServiceException(ResultCodeEnum.PARAM_ERROR);  //抛出参数不正确
                }
            }
            return true;
        }catch (Exception e){
            LOG.error(e);
            throw new ServiceException(ResultCodeEnum.SERVICE_ERROR);  //服务器异常
        }
    }
}




